Tuesday, 20 March 2012

Debugging UIWebView content in a MonoTouch app

Desktop browsers have great support for diagnosing rendering issues or scripting bugs.

But what are you supposed to do if you're developing an iPhone or iPad app, and you need to debug web content hosted inside a UIWebView control?

It turns out that you can use desktop Safari to "remotely" inspect mobile Safari when its running in the iOS Simulator. Happy days!

To pull this off your app has to call a private API, or at least it does in iOS 5.1 - hopefully Apple will expose this API "properly" at some point.

If you want to see how to do this in Objective-C head over to Nathan de Vries' post. If you prefer writing iOS apps in C# here's what to do:

First add the following method to your AppDelegate class:

[Conditional("DEBUG")]
private static void EnableUIWebViewRemoteInspector()
{
    var webViewClass = Class.GetHandle("WebView");
    var selector = Selector.GetHandle("_enableRemoteInspector");

    Messaging.void_objc_msgSend(webViewClass, selector);
}

You'll notice that EnableUIWebViewRemoteInspector() is conditionally compiled into DEBUG builds only. Your app will most likely be rejected by Apple if you submit it with calls to private APIs, so make sure you submit a RELEASE build and/or remove EnableUIWebViewRemoteInspector() once you're done debugging.

Next call the new method from your AppDelegate's FinishedLaunching method:

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{   
    EnableUIWebViewRemoteInspector();   
    return true;
}

That's it!

To try it out: fire up your app in the iOS Simulator and browse to http://localhost:9999 using desktop Safari (the remote inspector doesn't work properly in Chrome or Firefox).

You'll be presented with a list of pages you can inspect. When you pick one this is what you should see:

Wednesday, 1 February 2012

LLVM syntax highlighting in Sublime Text 2

If you use a Mac for any kind of development work you should give Sublime Text 2 a try.

It's a seriously awesome editor:
  • It's pretty.
  • It's fast.
  • It supports a good selection of languages.
  • It's got nifty features such as "goto anything" and "auto completion" that you usually only find in full featured IDEs. 
  • As an added bonus it's Australian made. 

Go and download it now!

Seriously :)

One of the languages that Sublime Text 2 doesn't support out of the box is LLVM's assembly language.  (i.e. syntax highlighting doesn't work for .ll files).


Adding LLVM support is really easy because Sublime Text 2 is more or less compatible with TextMate syntax definition files, and an LLVM TextMate bundle is already available on github.

The Sublime Text 2 site has good documentation on how to add new packages. The short story is that you can add LLVM support by doing the following:
  1. Start Sublime Text 2.
  2. Use the "Sublime Text 2/Preferences/Browse Packages" menu to open the packages folder in Finder.
  3. Create a new folder called LLVM (the name isn't really important - use another name if you prefer).
  4. Save llvm.tmbundle from github into your new folder.
Sublime Text 2 will start using the new package without a restart, so go ahead and open an .ll file or two :)